package com.tracelog.example.controller;

import com.tracelog.core.annotation.TraceLog;
import com.tracelog.core.enums.IncludeType;
import com.tracelog.core.enums.LogLevel;
import com.tracelog.example.model.User;
import com.tracelog.example.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 *
 * @author tracelog
 * @since 1.0.0
 */
@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
@TraceLog(module = "用户管理", level = LogLevel.INFO)
public class UserController {

    private final UserService userService;

    /**
     * 获取所有用户
     *
     * @return 用户列表
     */
    @GetMapping
    @TraceLog("获取所有用户")
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    /**
     * 根据ID获取用户
     *
     * @param id 用户ID
     * @return 用户信息
     */
    @GetMapping("/{id}")
    @TraceLog(value = "获取用户详情", level = LogLevel.DEBUG)
    public User getUserById(@PathVariable Long id) {
        return userService.getUserById(id);
    }

    /**
     * 创建用户
     *
     * @param user 用户信息
     * @return 创建后的用户
     */
    @PostMapping
    @TraceLog(value = "创建新用户", level = LogLevel.INFO, 
        includes = {IncludeType.REQUEST_BODY, IncludeType.RESPONSE_BODY},
        sensitiveFields = {"password", "email"})
    public User createUser(@RequestBody User user) {
        return userService.createUser(user);
    }

    /**
     * 更新用户
     *
     * @param id   用户ID
     * @param user 用户信息
     * @return 更新后的用户
     */
    @PutMapping("/{id}")
    @TraceLog(value = "更新用户信息", level = LogLevel.WARN)
    public User updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        return userService.updateUser(user);
    }

    /**
     * 删除用户
     *
     * @param id 用户ID
     */
    @DeleteMapping("/{id}")
    @TraceLog(value = "删除用户", level = LogLevel.ERROR)
    public void deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
    }

    /**
     * 模拟异常
     *
     * @return 不会返回
     */
    @GetMapping("/error")
    @TraceLog("模拟异常")
    public String simulateError() {
        throw new RuntimeException("模拟的异常情况");
    }
} 